<?php
namespace app\admin\controller;

use app\common\model\LuntanVote       as LuntanVoteModel;
use app\common\model\LuntanVoteselect as LuntanVoteselectModel;
use app\common\controller\AdminBase;

/**
 * 栏目管理
 * Class Category
 * @package app\admin\controller
 */
class LuntanVote extends AdminBase
{
	protected $vote_model;
	protected $voteselect_model;

	protected function _initialize()
	{
		parent::_initialize();
		$this->voteselect_model = new LuntanVoteselectModel();
		$this->vote_model 		= new LuntanVoteModel();
		$categorylist = $this->vote_model->getCategoryList();
		$this->assign('categorylist', $categorylist);
	}  

	/**
     * 投片管理页面
     * @return mixed
     */
	public  function  index($cid = 0, $keyword = '', $page = 1)
	{
		$votelist = $this->vote_model->getVoteList($cid,$keyword,$page);
		return $this->fetch('index',['cid'=>$cid,'keyword'=>$keyword,'page'=>$page,'votelist'=>$votelist]);
	}

	/**
     * 删除投票
     * @param int   $id
     * @param array $ids
     */
    public function delete($id = 0, $ids = [])
    {
    	$id = $ids ? array_merge($ids) : $id;
        if ($id) {
            if ($this->vote_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的投票');
        }
    }


    /**
     * 添加投票
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存
     */
    public function save()
    {	
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'LuntanVote');
            //需要判断投票选项select是否有值
            if(count($data['select']) == 1 && empty($data['select'][0])){
				$this->error('添加失败,请输入投票选项');
            }
            //model中操作
            $list = $this->vote_model->getDealList($data);
            if($list){
            	if (is_array($list)) {
            		$arr[] = $this->voteselect_model->saveAll($list);
            		if(count($arr)){
            			$this->success('保存成功');
            		}else{
            			$this->error('保存失败');
            		}
            	}
            }else{
            	$this->error('保存失败');
            }
        }
    }

    /**
     * 编辑邮票信息
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $vote = $this->vote_model->getVoteInfo($id);
        return $this->fetch('edit', ['vote' => $vote]);
    }

    /**
     * 更新文章
     * @param $id
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'LuntanVote');       
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
            	$result = $this->vote_model->updateDatabase($data);
                if ($result) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }
}